package com.huatai.datacenter.job;

import com.huatai.datacenter.entity.AllURLEntity;
import com.huatai.datacenter.service.AllURLService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.annotation.Annotation;
import java.util.Map;

/**
 * @author Lion
 * @date 2023/7/20  14:16
 */
@Slf4j
public class InterfaceListener implements Runnable{

	@Autowired
	private WebApplicationContext applicationContext;

	@Autowired
	private AllURLService allURLService;

	/**
	 * @author Lion
	 * @description 构造方法
	 * @param allURLService
	 * @param applicationContext
	 * @return
	 */
	public InterfaceListener(AllURLService allURLService,WebApplicationContext applicationContext){
		this.allURLService = allURLService;
		this.applicationContext = applicationContext;
	}

	/**
	 * @author Lion
	 * @description 每次启动程序，先情况原有的接口信息，从新写入所有接口信息
	 * @param
	 * @return void
	 */
	@Override
	public void run() {

		//清空所有接口
		allURLService.deleteAllURL();
		AllURLEntity entity = null;

		RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
		// 获取url与类和方法的对应信息
		Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();

		for (Map.Entry<RequestMappingInfo, HandlerMethod> mappingInfoHandlerMethodEntry : map.entrySet()) {

			entity = new AllURLEntity();
			RequestMappingInfo requestMappingInfo = mappingInfoHandlerMethodEntry.getKey();
			HandlerMethod handlerMethod = mappingInfoHandlerMethodEntry.getValue();
			// 类名
			entity.setClassName(handlerMethod.getMethod().getDeclaringClass().getName());
			Annotation[] parentAnnotations = handlerMethod.getBeanType().getAnnotations();
			for (Annotation annotation : parentAnnotations) {
				if (annotation instanceof Api) {
					Api api = (Api) annotation;
					//类描述
					entity.setClassDesc(api.value());
				} else if (annotation instanceof RequestMapping) {
					RequestMapping requestMapping = (RequestMapping) annotation;
					if (null != requestMapping.value() && requestMapping.value().length > 0) {
						//类URL
						entity.setClassURL(requestMapping.value()[0]);
					}
				}
			}
			// 方法名
			entity.setMethodName(handlerMethod.getMethod().getName());
			Annotation[] annotations = handlerMethod.getMethod().getDeclaredAnnotations();
			//Parameter[] methodParameters = handlerMethod.getMethod().getParameters(); //参数
			if (annotations != null) {
				// 处理具体的方法信息
				for (Annotation annotation : annotations) {
					if (annotation instanceof ApiOperation) {
						ApiOperation methodDesc = (ApiOperation) annotation;
						String desc = methodDesc.value();
						//接口描述
						entity.setApiName(desc);
					}
				}
			}
			PatternsRequestCondition p = requestMappingInfo.getPatternsCondition();
			for (String url : p.getPatterns()) {
				//请求URL
				entity.setPath(url);
			}
			RequestMethodsRequestCondition methodsCondition = requestMappingInfo.getMethodsCondition();
			for (RequestMethod requestMethod : methodsCondition.getMethods()) {
				//请求方式：POST/GET
				entity.setRequestType(requestMethod.toString());
			}
			allURLService.save(entity);
		}
	}
}
